package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.Listener.SubjectExcelListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ct_start
 * @since 2021-08-12
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    // 添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            // 文件输入流
            InputStream inputStream = file.getInputStream();
            // 调用方法进行读取
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();

        }catch (Exception e){
        e.printStackTrace();
        }

    }

    // 课程分类列表(树形)
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有一级分类
        QueryWrapper<EduSubject> WrapperOne=new QueryWrapper<>();
        WrapperOne.eq("parent_id","0");

        //在 service 层里面需要调用mapper,但是mp已经继承了那个类,并且实现了mapper 的注入
        List<EduSubject> OneSubjectList = baseMapper.selectList(WrapperOne);

        // 查询所有二级分类
        QueryWrapper<EduSubject> WrapperTwo=new QueryWrapper<>();
        WrapperTwo.ne("parent_id","0");

        //在 service 层里面需要调用mapper,但是mp已经继承了那个类,并且实现了mapper 的注入
        List<EduSubject> twoSubjectList = baseMapper.selectList(WrapperTwo);


        // 创建最后一个返回的数组集合
        List<OneSubject> finalSubjectList=new ArrayList<>();

        // 封装一级分类
        for (int i = 0; i < OneSubjectList.size(); i++) {

            EduSubject eduSubject = OneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();

            BeanUtils.copyProperties(eduSubject,oneSubject);
            // 创建一个封装二级分类的数组

            finalSubjectList.add(oneSubject);
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {

                EduSubject tSubject = twoSubjectList.get(m);
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);

                }
            }

            oneSubject.setChildren(twoFinalSubjectList);

        }




        return finalSubjectList;
    }
}
